﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iOak.Domain.ReportDomain;
using iOak.Business.Resumes;
using iOak.Business.Campaigns;
using iOak.Business.Users;
using iOak.Core;

namespace iOak.Business.Report
{
    public class CollegeReportService
    {
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly CompanyService itemCompanyService = new CompanyService();
        private static readonly UserService itemUserService = new UserService();
        private static readonly ApplicationCandidateService itemApplicationCandidateService = new ApplicationCandidateService();
        private static readonly ResumeDetailService itemResumeDetailService = new ResumeDetailService();
        public List<CollegeDeital> GetDetialList(Guid campaignId, Guid companyId)
        {
            List<CollegeDeital> returnList = new List<CollegeDeital>();
            var currentList = GetDetialList(campaignId, typeof(CollegeDeital),companyId);
            returnList = currentList.Select(m => (CollegeDeital)m).ToList();
            return returnList;
        }

        public decimal GetSumBounty(List<CollegeDeital> listDetial, decimal Bounty)
        {
            decimal sumBounty = 0;

            foreach (var item in listDetial)
            {
                var startTime = Convert.ToDateTime(item.ContractStartDate);
                var endTime = Convert.ToDateTime(item.ContractEndDate);
                int month = CommonHelper.GetMonth(startTime, endTime);
                if (month == 0)
                {
                    month = 1;
                }
                sumBounty += month * Bounty;
            }

            return sumBounty;
        }


        public List<dynamic> GetDetialList(Guid campaignId, Type currentType, Guid companyId)
        {
            var returnList = new List<dynamic>();
            var itemApplication = itemApplicationService.GetListByCampaignIdAndCompanyId(campaignId, companyId);
            int number = 1;
            if (itemApplication != null)
            {
                // 获取候选人列表
                var listApplicationCandidates = itemApplicationCandidateService.GetPassedListByApplicationId(itemApplication.ApplicationId);

                // 根据候选人列表获取报表的对象
                listApplicationCandidates.ForEach(item =>
                {
                    // 获取设定的值列表
                    var listResumeDetail = itemResumeDetailService.GetListByResumeId(item.ResumeId);

                    // 创建实例
                    dynamic addItem = Activator.CreateInstance(currentType);
                    addItem.Number = number++;

                    // 获取属性列表          
                    var itemProperties = currentType.GetProperties().ToList();

                    itemProperties.ForEach(itemPropertie =>
                    {
                        // 获取当前key的值
                        var itemResumeDetail = listResumeDetail.FirstOrDefault(m => m.Key.Equals(itemPropertie.Name));
                        if (itemResumeDetail != null)
                        {
                            itemPropertie.SetValue(addItem, itemResumeDetail.ValueString, null);
                        }
                    });

                    returnList.Add(addItem);
                });
            }

            return returnList;
        }
    }
}
